<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT open_shop, prizes_awarded " .
		"FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$open_shop = $row[ 'open_shop' ];
$prizes_awarded = $row[ 'prizes_awarded' ];

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
	if( $prizes_awarded == 0 && $open_shop == 1 ) {
		$turn = getCurrentTurn();
		$date = getCurrentDate();
		$header = 	'From: premium@agesofstrife.com' . "\r\n" .
					'Reply-To: premium@agesofstrife.com' . "\r\n" .
					'X-Mailer: PHP/' . phpversion();	
		$query = "SELECT users.user_id, ruler.ruler_id, ruler.name, users.email, users.premium, ruler.economy_points " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 " .
				"ORDER BY economy_points DESC " .
				"LIMIT 3";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$top3 = "The top 3 economy points:\n";
		$i = 0;
		while( $i < $num ) {
			if( $i == 0 ) {
				$top3 .= "1st: ";
			}
			elseif( $i == 1 ) {
				$top3 .= "2nd: ";
			}
			else {
				$top3 .= "3rd: ";
			}
			$top3 .= mysql_result( $result, $i, "name" ) . " with " . number_format( mysql_result( $result, $i, "economy_points" ) ) . " economy points.\n";
			$i ++;
		}
	
		$i = 0;
		while( $i < $num ) {
			if( $i == 0 ) {
				$prize = "1st prize";
				$turns = 1000;
			}
			elseif( $i == 1 ) {
				$prize = "2nd prize";
				$turns = 600;
			}
			else {
				$prize = "3rd prize";
				$turns = 400;
			}
			
			$premium = mysql_result( $result, $i, "premium" );
			if( $premium < 0 ) {
				$premium = 0;
			}
			
			$query2 = "UPDATE users " .
					"SET premium = ( $premium + $turns ) " .
					"WHERE user_id = " . mysql_result( $result, $i, "user_id" );
			mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			
			$subject = "Congratulations: $prize for economy points";
			$news = "Congratulations, you have received the $prize for your economy points.\n" .
					"As a reward " . number_format( $turns ) . " premium turns have been added to your account.\n\n$top3";
			$query2 = "INSERT INTO news " .
					"( ruler_id, type, news, date, subject, seen, turn ) " .
					"VALUES ( " . mysql_result( $result, $i, "ruler_id" ) . ", 2, '" . addslashes( $news ) . "', '$date', '" . addslashes( $subject ) . "', 0, $turn )";
			mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			
			if( mysql_result( $result, $i, "email" ) != '' ) {
				$message = "Congratulations, you have received the $prize for your economy points in Ages of Strife.\n" .
						"As a reward " . number_format( $turns ) . " premium turns have been added to your account.\n\n" . $top3 .
						"\nTo check out your premium turns log in to Ages of Strife at: http://www.agesofstrife.com";
				mail( mysql_result( $result, $i, "email" ), $subject, $message, $header );
			}
			
			$i ++;
		}
		
		$query = "SELECT users.user_id, ruler.ruler_id, ruler.name, users.email, users.premium, ruler.warrior_points " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 " .
				"ORDER BY warrior_points DESC " .
				"LIMIT 3";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$top3 = "The top 3 war points:\n";
		$i = 0;
		while( $i < $num ) {
			if( $i == 0 ) {
				$top3 .= "1st: ";
			}
			elseif( $i == 1 ) {
				$top3 .= "2nd: ";
			}
			else {
				$top3 .= "3rd: ";
			}
			$top3 .= mysql_result( $result, $i, "name" ) . " with " . number_format( mysql_result( $result, $i, "warrior_points" ) ) . " war points.\n";
			$i ++;
		}
	
		$i = 0;
		while( $i < $num ) {
			if( $i == 0 ) {
				$prize = "1st prize";
				$turns = 1000;
			}
			elseif( $i == 1 ) {
				$prize = "2nd prize";
				$turns = 600;
			}
			else {
				$prize = "3rd prize";
				$turns = 400;
			}
			
			$premium = mysql_result( $result, $i, "premium" );
			if( $premium < 0 ) {
				$premium = 0;
			}
			
			$query2 = "UPDATE users " .
					"SET premium = ( $premium + $turns ) " .
					"WHERE user_id = " . mysql_result( $result, $i, "user_id" );
			mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			
			$subject = "Congratulations: $prize for war points";
			$news = "Congratulations, you have received the $prize for your war points.\n" .
					"As a reward " . number_format( $turns ) . " premium turns have been added to your account.\n\n$top3";
			$query2 = "INSERT INTO news " .
					"( ruler_id, type, news, date, subject, seen, turn ) " .
					"VALUES ( " . mysql_result( $result, $i, "ruler_id" ) . ", 2, '" . addslashes( $news ) . "', '$date', '" . addslashes( $subject ) . "', 0, $turn )";
			mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			
			if( mysql_result( $result, $i, "email" ) != '' ) {
				$message = "Congratulations, you have received the $prize for your war points in Ages of Strife.\n" .
						"As a reward " . number_format( $turns ) . " premium turns have been added to your account.\n\n" .
						$top3 . 
						"\nTo check out your premium turns log in to Ages of Strife at: http://www.agesofstrife.com";
				mail( mysql_result( $result, $i, "email" ), $subject, $message, $header );
			}
			
			$i ++;
		}

		$timestamp = time();
		$query = "SELECT users.user_id " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 " .
				"ORDER BY economy_points DESC " .
				"LIMIT 25";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$i = 0;
		while( $i < $num ) {
			if( $i < 3 ) {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Economic gold star', 3, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			elseif( $i < 10 ) {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Economic silver star', 2, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			else {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Economic bronze star', 1, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			$i ++;
		}
		
		$query = "SELECT users.user_id " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 " .
				"ORDER BY warrior_points DESC " .
				"LIMIT 25";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$i = 0;
		while( $i < $num ) {
			if( $i < 3 ) {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Military gold star', 3, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			elseif( $i < 10 ) {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Military silver star', 2, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			else {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Military bronze star', 1, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			$i ++;
		}
		
		$query = "SELECT users.user_id, ( SELECT count(*) FROM village WHERE ruler_id = ruler.ruler_id ) AS village_count " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 AND " .
					"( ( SELECT count(*) FROM village WHERE ruler_id = ruler.ruler_id ) >= 20 OR ( SELECT count(*) FROM village WHERE ruler_id = ruler.ruler_id ) >= 40 )";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$i = 0;
		while( $i < $num ) {
			if( mysql_result( $result, $i, "village_count" ) >= 40 ) {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Grand expander medal', 3, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			elseif( mysql_result( $result, $i, "village_count" ) >= 20 ) {
				$query = "INSERT INTO medals " .
						"( title, value, user_id, timestamp ) " .
						"VALUES( 'Expander medal', 2, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			$i ++;
		}

		$query = "SELECT users.user_id " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 AND " .
					"( SELECT SUM(population) FROM village WHERE ruler_id = ruler.ruler_id ) >= 10000000";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$i = 0;
		while( $i < $num ) {
			$query = "INSERT INTO medals " .
					"( title, value, user_id, timestamp ) " .
					"VALUES( 'Peoples protector ribbon', 2, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$i ++;
		}
		
		$query = "SELECT round_name, turns_inactive " .
				"FROM game";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$round_name = $row[ 'round_name' ];
		$turns_to_inactive = $row[ 'turns_inactive' ];
		$turns_to_inactive = $turn - $turns_to_inactive;
		
		$query = "SELECT users.user_id " .
				"FROM ruler " .
				"JOIN users ON ruler.user_id = users.user_id " .
				"WHERE resigned = 0 AND banned = 0 AND active_turn > $turns_to_inactive";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$i = 0;
		while( $i < $num ) {
			$query = "INSERT INTO medals " .
					"( title, value, user_id, timestamp ) " .
					"VALUES( '" . $round_name . " campaign medal', 2, " . mysql_result( $result, $i, "user_id" ) . ", $timestamp )";
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$i ++;
		}
				
		$query2 = "UPDATE game " .
				"SET prizes_awarded = 1";
		mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	}

	header( "Location: admin.php" );
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>
